package com.hnkywl.sts.dao.organ;

import com.hnkywl.sts.entity.base.Lxr;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-23
 * Time: 下午1:34
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class LxrDao extends SimpleHibernateTemplate<Lxr,Long> {
    /**
     * 查询联系人列表
     * @param xm       联系人名称
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Lxr> findLxrList(String xm,int pageNo, int pageSize){
        Page<Lxr> page = new Page<Lxr>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("lastUpdated");
        if(StringUtils.isNotEmpty(xm)){
            return this.findByCriteria(page,new Criterion[]{Restrictions.like("xm", xm, MatchMode.START)});
        }else
            return this.findAll(page);
    }

    /**
     * 联系人重复验证
     * @param id
     * @param propertyValue
     * @param propertyName
     * @return
     */
    public Boolean checkUnique(Long id, Object propertyValue,String propertyName) {
        String hql;
        if (propertyValue == null)
            return false;
        if(id == null){
            //修改检验
            hql = "from Lxr where " + propertyName + " = ? ";
            return this.existsQuery(hql, propertyValue);
        }else{
            //新增检验
            hql = "from Lxr where " + propertyName + " = ? and id <> ?";
            return this.existsQuery(hql, propertyValue,id);
        }
    }
}
